<?php 
	jimport('joomla.application.component.controller');
	//require 'components/com_kinh_doanh/tables/kinh_doanh.php';

	
	class sale_tsc_thu_nganControllersu_luoc_thu_ngan extends JController
	{
	   function xoa()
       {
            $db = &JFactory::getDBO();
            $post = JRequest::get('post',4);
            print_r($post);
            if($post['flag']=='1')
            {
                //echo "co";die;
                $con = $_SESSION['config'];
                $id_wh = $con->id_warehouse; 
                $model = $this->getModel("su_luoc_thu_ngan");
                $id_bill =  $post['id_bill'];
                $model->xoa_buz($id_bill);
                $data = $model->ds_bill($id_bill);
                //print_r($data);
                $model->cap_nhat_stt_sale_order($id_bill);
                foreach($data as $row)
                {
                    
                    $model->cap_nhat_lai_so_luong($row->id_pro,$id_wh,$row->number);
                }
            }
            
            //print_r($post);die;
            //$row =  & JTable::getInstance('erp_sale', 'Table'); 
            //$row->del =1;
            //$row->who_del = $_SESSION['id_user'];
            //$row->bind('post');
            //$row->store('39');
            $sql = "UPDATE #__erp_sale SET
                                del = 1,
                                who_del = '".$_SESSION['id_user']."'
                                WHERE 
                                id = '".$post['id_bill']."' ";
                                $db->setQuery($sql);
                if(!$db->query())
                {
                    print_r($db);die;
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","Thanh cong");
                return;
       }
       //xu ly phu thu
        function xu_ly_phu_thu()
        {   $db = &JFactory::getDBO();
            $post = JRequest::get('post',4);
            $row =  & JTable::getInstance('erp_sale', 'Table'); 
            //echo JRequest::getInt('txtphatsinh');die;
            if(JRequest::getInt('txtphatsinh')==0 && JRequest::getInt('txtkhachtra')==0)
            {
                $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan");
                return;
            }
            $row->total = $post['txtphatsinh'];
            $row->cus_paid = $post['txtkhachtra'];
            $row->note = $post['txtnote'];
            if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                                  	     JError::raiseError(500, $row->getError() );
                                         return;
                                            }
            $sql = "select * from #__erp_agent_branch_account where id_a_b = 0";
                $db->setQuery($sql);
                $dt = $db->loadObject();
                $ba = $dt->balance + $post['txtphatsinh'];
                
                $sql = "UPDATE #__erp_agent_branch_account SET
                                balance = '".$ba."'
                                WHERE 
                                id_a_b =0 ";
                                $db->setQuery($sql);
                if(!$db->query())
                {
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","Thanh cong.");
                return;
        }
        //hoan tat don hang
        function hoan_tat()
        {
            $post = JRequest::get('post',4);
            print_r($post);
            //cap nhat lai don hang theo bill,stt = 4
            $db = &JFactory::getDBO();
            $sql = "UPDATE #__erp_sale SET
                                status = 4,
                                id_currency = '".$post['id_cu']."',
                                id_cuppon = '".$post['idcup']."',
                                id_customer = '".$post['txtmkh']."',
                                total = '".$post['txtsum']."',
                                cus_paid = '".$post['txtkhachdua']."',
                                orther = 0
                                WHERE 
                                id ='".$post['id_bill']."' ";
                                $db->setQuery($sql);
                           //     $db->query();
            if(!$db->query())
            {
                $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                return;
            }
            if($post['idcup']!='')
            {
                //echo "co gia tri";
                $sql = "UPDATE #__erp_cuppon SET
                                used = 1
                                WHERE 
                                id ='".$post['idcup']."' ";
                                $db->setQuery($sql);
                 if(!$db->query())
                {
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            } 
            
            if($post['txtmkh']!='')
            {
                $sql = "select * from #__erp_product_custom_account where id_cus = '".$post['txtmkh']."'";
                $db->setQuery($sql);
                $dt = $db->loadObject();
                $ba = $dt->balance - $post['txtsum'];
                
                $sql = "UPDATE #__erp_product_custom_account SET
                                balance = '".$ba."'
                                WHERE 
                                id_cus ='".$post['txtmkh']."' ";
                                $db->setQuery($sql);
                if(!$db->query())
                {
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            }
            
            $sql = "select * from #__erp_agent_branch_account where id_a_b = 0";
                $db->setQuery($sql);
                $dt = $db->loadObject();
                $ba = $dt->balance + $post['txtsum'];
                
                $sql = "UPDATE #__erp_agent_branch_account SET
                                balance = '".$ba."'
                                WHERE 
                                id_a_b =0 ";
                                $db->setQuery($sql);
                if(!$db->query())
                {
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            $sql = "DELETE FROM #__erp_sale_order_buzy
                        WHERE id_order= '".$post['id_buz']."'";
                                $db->setQuery($sql);
            if(!$db->query())
                {
                    $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","That bai");
                    return;
                }
            $this->setRedirect("index.php?option=com_sale_tsc_thu_ngan","Thanh cong");
            return;
        }	   
        //chuyen sang don hang
        function thanh_toan()
        {
            
            JRequest::setVar('view','thanh_toan'); 
			parent::display();	 // hien thi
        }	
           
		//neu khong co task nao phu hop thi mac se chay function nay
		function display()
		{
			JRequest::setVar('view','su_luoc_thu_ngan'); 
			parent::display();	 // hien thi
		}
        
        
    }
?>